<?php

namespace app\batch;

use think\Db;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{IntergralGoods, IntergralOrdergoods, Order, Orderlog, Riderlist, RiderStreet, Region};

class Replar extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Replar')->setDescription('图片链接转换');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        // (new Order())->where(['status' => 3, 'weight' => 0])->chunk(20, function ($list) {
        //     //查询下级订单的总重
        //     foreach ($list as $lists) {
        //         $weight = (new Orderlog())->where('ordersn', $lists['ordersn'])->sum('weight') ?? 0;
        //         Db::startTrans();
        //         try {
        //             (new Order())->where('id', $lists['id'])->update(['weight' => $weight]);
        //             Db::commit();
        //         } catch (\Exception $e) {
        //             dump($e->getMessage());
        //             Db::rollback();
        //             echo '更改失败';
        //             die;
        //         }
        //     }
        // });
        // $list = (new Region())->where('level', 3)->select();
        // foreach ($list as $lists) {
        //     Db::startTrans();
        //     try {
        //         (new Region())->where(['parent_id' => $lists['id'], 'level' => 4])->update(['area_code' => $lists['area_code']]);
        //         Db::commit();
        //     } catch (\Exception $e) {
        //         dump($e->getMessage());
        //         Db::rollback();
        //         echo '更改失败';
        //         die;
        //     }
        // }
        $list = (new Order())->where(['area' => [340121]])->select();
        foreach ($list as $lists) {
            Db::startTrans();
            try {
                (new Orderlog())->where('ordersn', $lists['ordersn'])->delete();
                (new Order())->where('id', $lists['id'])->delete();
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                echo '删除失败';
                die;
            }
            echo '删除成功';
        }
        $list = (new RiderStreet())->where(['areaid' => [340121]])->select();
        foreach ($list as $lists) {
            Db::startTrans();
            try {
                (new RiderStreet())->where('id', $lists['id'])->delete();
                $have = (new Riderlist())->where('id', $lists['riderid'])->find();
                if (!empty($have)) {
                    (new Riderlist())->where('id', $lists['riderid'])->delete();
                }
                Db::commit();
            } catch (\Exception $e) {
                dump($e->getMessage());
                Db::rollback();
                echo '删除失败';
                die;
            }
        }

        // //订单详情分类图片修改
        // (new Orderlog())->chunk(1000, function ($lists) {
        //     foreach ($lists as $list) {
        //         $list['icon'] = str_replace("garbage.vj1tf.cn", "apis.dida555.com", $list['icon']);
        //         $res = (new Orderlog())->where('id', $list['id'])->update(['icon' => $list['icon']]);
        //         if ($res) {
        //             echo '成功';
        //         }
        //     }
        // });
        // //骑手认证图片想修改
        // (new Riderlist())->chunk(1000, function ($lists) {
        //     foreach ($lists as $list) {
        //         $list['positive'] = str_replace("garbage.vj1tf.cn", "apis.dida555.com", $list['positive']);
        //         $list['negative'] = str_replace("garbage.vj1tf.cn", "apis.dida555.com", $list['negative']);
        //         $res = (new Riderlist())->where('id', $list['id'])->update(['positive' => $list['positive'], 'negative' => $list['negative']]);
        //         if ($res) {
        //             echo '成功';
        //         }
        //     }
        // });
        // //积分商城订单图片想修改
        // (new IntergralOrdergoods())->chunk(1000, function ($lists) {
        //     foreach ($lists as $list) {
        //         $list['images'] = json_decode($list['images'], true);
        //         $imagesarr = [];
        //         $detailarr = [];
        //         foreach ($list['images'] as $k =>  $images) {
        //             $images = str_replace("garbage.vj1tf.cn", "apis.dida555.com", $images);
        //             $imagesarr[] = $images;
        //         }
        //         $list['images'] = json_encode($imagesarr, true);
        //         $res = (new IntergralOrdergoods())->where('id', $list['id'])->update(['images' => $list['images']]);
        //         if ($res) {
        //             echo '成功';
        //         }
        //     }
        // });
        // //积分商城图片想修改
        // (new IntergralGoods())->chunk(1000, function ($lists) {
        //     foreach ($lists as $list) {
        //         $list['images'] = json_decode($list['images'], true);
        //         $list['detail'] = json_decode($list['detail'], true);
        //         $imagesarr = [];
        //         $detailarr = [];
        //         foreach ($list['images'] as $k =>  $images) {
        //             $images = str_replace("garbage.vj1tf.cn", "apis.dida555.com", $images);
        //             $imagesarr[] = $images;
        //         }
        //         foreach ($list['detail'] as $k =>  $images) {
        //             $images = str_replace("garbage.vj1tf.cn", "apis.dida555.com", $images);
        //             $detailarr[] = $images;
        //         }
        //         $list['images'] = json_encode($imagesarr, true);
        //         $list['detail'] = json_encode($detailarr, true);
        //         $res = (new IntergralGoods())->where('id', $list['id'])->update(['images' => $list['images'], 'detail' => $list['detail']]);
        //         if ($res) {
        //             echo '成功';
        //         }
        //     }
        // });
    }
}
